fix(fsapp): drop on_final from turn hook so exit_reason no longer finalizes the task (#685) - #760
Open
Kailigithub wants to merge 1 commit into
Open
Conversation
…alizes the task (lsdefine#685) A turn-level exit_reason can fire on an intermediate agent turn, not only on the queued task's real completion. The previous _make_task_hook used that signal to call _finish early, which set result['sent'] = True before the display queue published its real {done: ...} item. Cards could therefore be marked complete with the wrong text and final attachment handling could run against an unfinished task. Make the hook summary-only: it patches the per-turn card step from summary and never finalizes the task. Finalization now lives only on the display-queue done item (and on the timeout/stop/exception paths in run_agent, which were unchanged). Adds tests/test_fsapp_exit_reason_no_finalize.py with 6 cases that exercise the live _make_task_hook plus a behavioral pin of the old on_final branch. Fixes lsdefine#685.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_make_task_hookinfrontends/fsapp.pypreviously treated any turn-levelexit_reasonas completion of the whole Feishu task and invoked the task'son_finalcallback (_finish) immediately. That callback setsresult["sent"] = True, sorun_agentcould leave its display-queue loop before the queue published its real{"done": ...}item. The card could be marked complete from an intermediate turn response, and final generated-file/attachment handling could run with the wrong text.The same hook also had a second bug: when a single context contained both
exit_reasonandsummary, theexit_reasonbranch won, so the per-turn step was not added to the card.This PR makes the hook summary-only — it only patches the per-turn card step from
summary. Finalization is now reachable only via the display-queuedoneitem (and the timeout/stop/exception paths inrun_agent, which were unchanged).Fixes #685.
Root cause
_make_task_hook(card, task_id, on_final)was passed_finishason_final. The innerhookthen firedon_finalwheneverctx['exit_reason']was truthy. Becauseexit_reasonis a per-turn signal (seeagent_loop.py:73-92—exit_reasonis set inside the per-turn outcome loop, not the per-task one), an intermediate turn-end would prematurely finalize the queued task.Reproduction
Issue #685 includes an isolated harness exercising
_make_task_hook:After this fix, both
on_finalcalls disappear (finalization belongs to the queue'sdoneitem) and the summary is preserved as a card step.Change
_make_task_hook(card, task_id, on_final)→_make_task_hook(card, task_id)hookdrops theexit_reasonbranch and theon_final(raw)invocation. Thesummarybranch is unchanged.run_agentno longer passes_finish. The display-queue loop'sif item and "done" in item: await asyncio.to_thread(_finish, item.get("done", ""))path is the sole_finishentrypoint (alongside timeout/stop/exception, which were untouched).Net diff: 12 insertions, 8 deletions in
frontends/fsapp.py.Test
tests/test_fsapp_exit_reason_no_finalize.pycovers:_finish, no step (the core [fsapp] turn-end exit_reason can finalize a queued task before display queue done #685 case)._finish._finish._make_task_hooksignature no longer carrieson_final.fsapp.pyever regains an on_final path.The tests load the live
_make_task_hookAST source so they exercise the actual upstream function (not a copy) and can be run withpython tests/test_fsapp_exit_reason_no_finalize.pyon a fresh clone (no pytest, nonode_modules, no Feishu credentials).Verification
The same suite was also run against the unfixed source (
git checkout main -- frontends/fsapp.py) and correctly reports the missingon_finalargument / old signature.Closes #685